/**
 * +======================================================================================================================+
 * @Author: CimZzz
 * @Date: 2023-03-07 14:15:00
 * 
 * 代码分割块，以下内容涉及: 
 * 
 * 信号检查和拦截
 * 
 */
#include <cstdio>
#include <csignal>
#include <cstdlib>
#include <unistd.h>

using namespace std;
   asdzxczxc
static int count = 0;

void sig_block_check() {
	sigset_t set;
	if(sigprocmask(0, NULL, &set) == -1) {
		printf("check failed\n");
		return;
	}
	if(sigismember(&set, SIGINT)) {
		printf("recv SIGINT\n");
	} else {
		printf("recv NULL\n");
	}
}

void sig_block() {
	sigset_t set;
	sigaddset(&set, SIGINT);
	if(sigprocmask(SIG_BLOCK, &set, NULL) == -1) {
		printf("block failed\n");
		return;
	}
	printf("blocked\n");
}

void sig_unblock() {
	sigset_t set;
	sigaddset(&set, SIGINT);
	if(sigprocmask(SIG_UNBLOCK, &set, NULL) == -1) {
		printf("unblock failed\n");
		return;
	}
	printf("unblocked\n");
}

void sig_int(int sig) {
	sig_block_check();
	sig_unblock();
	if(::count ++ > 3) {
		exit(2);
	}
}


int main(int argc, char ** argv) {
	// signal(SIGINT, sig_int);
	sig_block_check();
	sig_block();
	sig_block_check();
	sleep(10);
	sig_block_check();
	sig_unblock();
	sig_block_check();
	sleep(10);
	sig_block_check();
	// while(1) {
	// 	printf("do loop\n");
	// 	sleep(3);
	// }

	return 0;
}